/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the koala4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * @Author: GuoYang
 */

package com.koalacloud.koala.zhaikucloud.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koalacloud.koala.common.core.util.R;
import com.koalacloud.koala.common.log.annotation.SysLog;
import com.koalacloud.koala.zhaikucloud.entity.MallProduct;
import com.koalacloud.koala.zhaikucloud.entity.MallProductType;
import com.koalacloud.koala.zhaikucloud.service.MallProductTypeService;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 商品类目
 *
 * @Author: GuoYang
 * @date 2022-02-21 20:14:41
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/mallProductType")
@Tag(description = "mallProductType" , name = "商品类目管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class MallProductTypeController {

    private final  MallProductTypeService mallProductTypeService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param mallProductType 商品类目
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('mallProductType_view')")
    public R getMallProductTypePage(Page page, MallProductType mallProductType) {
        return R.ok(mallProductTypeService.page(page, Wrappers.query(mallProductType)));
    }


    /**
     * 通过id查询商品类目
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('mallProductType_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(mallProductTypeService.getById(id));
    }

    /**
     * 新增商品类目
     * @param mallProductType 商品类目
     * @return R
     */
    @Operation(summary = "新增商品类目", description = "新增商品类目")
    @SysLog("新增商品类目")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('mallProductType_add')")
    public R save(@RequestBody MallProductType mallProductType) {
        return R.ok(mallProductTypeService.save(mallProductType));
    }

    /**
     * 修改商品类目
     * @param mallProductType 商品类目
     * @return R
     */
    @Operation(summary = "修改商品类目", description = "修改商品类目")
    @SysLog("修改商品类目")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('mallProductType_edit')")
    public R updateById(@RequestBody MallProductType mallProductType) {
        return R.ok(mallProductTypeService.updateById(mallProductType));
    }

    /**
     * 通过id删除商品类目
     * @param ids id 列表
     * @return R
     */
    @Operation(summary = "通过id删除商品类目", description = "通过id删除商品类目")
    @SysLog("通过id删除商品类目")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('mallProductType_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(mallProductTypeService.removeBatchByIds(CollUtil.toList(ids)));
    }

    @GetMapping(value = "/tree")
    public R<List<Tree<Integer>>> getTree(boolean lazy, Integer parentId) {
        return R.ok(mallProductTypeService.getTree(lazy, parentId));
    }

    /**
     * 导出excel 表格
     * @param mallProductType 查询条件
     * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('mallProduct_export')" )
    public List<MallProductType> export(MallProductType mallProductType, String[] ids) {
        return mallProductTypeService.list(Wrappers.lambdaQuery(mallProductType).in(ArrayUtil.isNotEmpty(ids), MallProductType::getId, ids));
    }
}
